< previous page page_172 next page >

Page 172
0172-01.jpg
Figure 8.3
The Object Browser shows you the value and
function of most of Visual Basic's internal constants.
Creating Your Own Constants
Although Visual Basic defines a large number of constants for many activities, sometimes you need to define your own constants. Constants are defined using the Const statement to give the constant a name and a value, as illustrated in the following syntax:
[Public | Private] Const constantname [As constanttype] = value
If you think this statement looks similar to the declaration of a variable, you're right. As with declaring a variable, you provide a name for the constant and, optionally, specify the type of data it should hold. The Const keyword at the beginning of the statement tells Visual Basic that this statement defines a constant. This keyword distinguishes the statement from one that just assigns a value to a variable. In declaring the type of a constant, you use the same intrinsic types as you do for defining variables. (These types are defined in Table 8.2.) Finally, to define a constant, you must include the equal sign (=) and the value to be assigned. If you are defining a string constant or date constant, remember to enclose the value in either quotation marks (") or the pound sign (#), respectively.
A constant's scope is also important. The same rules for the scope of variables, which were discussed in the earlier section ''Determining Where a Variable Can Be Used," apply to constants as well.
From Here . . .
This chapter provided you with an overview of using variables and constants in Visual Basic. In Chapter 11, "Managing Your Project: Sub Procedures, Functions, and Multiple Forms" you learn the fundamentals of writing the code that comprises the gist of your Visual Basic applications. For some more material on programming, refer to the following chapters:
Read Chapter 9, "Visual Basic Programming Fundamentals" to learn more about the Visual Basic Language.
To learn about the many different events available in the custom controls, see Chapter 12, "Microsoft Common Controls."

 
< previous page page_172 next page >